package com.br.fmn.clubedoimovel.negocio.map;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class RouteOverlay extends Overlay {
	private Paint paint;
	private Collection<GeoPoint> geoPoints;
	private MapView mapView;
	private Projection translateCoordinatesOnScreenMap;

	public RouteOverlay(Collection<GeoPoint> geoPoints) {
		this.geoPoints = geoPoints;
		this.paint = new Paint();
		this.paint.setColor(Color.RED);
		this.paint.setStrokeWidth(5);
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, shadow);

		this.mapView = mapView;
		List<GeoPoint> geoPoints = convertGeoPointToPoint();

		Point firstPoint, secondPoint;
		for (int i = 0; i < geoPoints.size() - 1; i++) {
			firstPoint = translateCoordinatesOnScreenMap.toPixels(
					geoPoints.get(i), null);
			secondPoint = translateCoordinatesOnScreenMap.toPixels(
					geoPoints.get(i + 1), null);

			// Desenha uma linha de um ponto a outro
			canvas.drawLine(firstPoint.x, firstPoint.y, secondPoint.x,
					secondPoint.y, paint);
		}
	}

	/**
	 * Esse objeto converte GeoPoint em Point para poder desenhar na tela
	 * 
	 * @return
	 */
	private List<GeoPoint> convertGeoPointToPoint() {

		this.translateCoordinatesOnScreenMap = mapView.getProjection();
		List<GeoPoint> geoPoints = new ArrayList<GeoPoint>(this.geoPoints);
		return geoPoints;
	}
}
